package com.bestcem.xm.txtanls.grpc.client;


import com.bestcem.xm.common.core.constant.GrpcClientNames;
import com.bestcem.xm.qdes.grpc.v1.services.*;
import com.bestcem.xm.txtanls.service.dto.survey.SurveyDTO;
import lombok.extern.slf4j.Slf4j;
import net.devh.boot.grpc.client.inject.GrpcClient;
import org.springframework.stereotype.Service;
import zhongyan.xm.base.v1.Base;

import java.util.List;
import java.util.Objects;

/**
 * 倍市得 qdes 模块相关rpc接口客户端
 *
 * @author XiaoYunSong <ys.xiao@idiaoyan.com>
 * @version v1.0
 * @date 2021/6/21 16:26
 */
@Slf4j
@Service
public class BaseQdesGrpcService {

    @GrpcClient(GrpcClientNames.DS_QDES)
    private BaseQdesServiceGrpc.BaseQdesServiceBlockingStub baseQdesServiceBlockingStub;

    @GrpcClient(GrpcClientNames.DS_QDES)
    private BpProjectServiceGrpc.BpProjectServiceBlockingStub bpProjectServiceBlockingStub;
    /**
     * 获取问卷项目信息
     *
     * @param surveyId 问卷id
     * @return Survey project info
     */
    public SurveyDTO getSurveyProject(String surveyId) {
        GetProjectRequest.Builder builder = GetProjectRequest.newBuilder();
        builder.setId(surveyId);
        try {
            ProjectResponse response = baseQdesServiceBlockingStub.getProject(builder.buildPartial());
            if (Objects.isNull(response) || Base.Code.ERROR.equals(response.getStatus().getCode())) {
                log.error("[Txtanls] QDES getSurveyProject Error; SurveyId: {} ErrorMsg: {}", surveyId, response);
                return null;
            }
            return responseToSurveyDTO(response);
        } catch (Exception e) {
            log.error("[Txtanls] QDES getSurveyProject Error; SurveyId: {}", surveyId, e);
            return null;
        }
    }

    /**
     * 获取问卷项目信息
     * 用不到了, 这个接口没接过来
     *
     * @param surveyId 问卷id
     * @return Survey project info
     */
    public List<OpenQuestion> getProjectAllOpenOption(String surveyId) {
        ListOpenOptionsRequest.Builder builder = ListOpenOptionsRequest.newBuilder();
        builder.setProjectId(surveyId);
        try {
            ListOpenOptionsResponse response = bpProjectServiceBlockingStub.listOpenOptions(builder.buildPartial());
            return response.getOpenQuestionsList();
        } catch (Exception e) {
            log.error("[Txtanls] QDES getProjectAllOpenOption Error; SurveyId: {}", surveyId, e);
            return null;
        }
    }


    // ========================== private model convert ==========================


    /**
     * ProjectResponse to SurveyDTO
     *
     * @param response ProjectResponse
     * @return SurveyDTO
     * @author XiaoYunSong <ys.xiao@idiaoyan.cn>
     * @date 2021/8/2 14:40
     */
    private SurveyDTO responseToSurveyDTO(ProjectResponse response) {
        Project project = response.getProject();
        if (Objects.isNull(project)) {
            return null;
        }
        SurveyDTO surveyDTO = new SurveyDTO();
        surveyDTO.setId(project.getId());
        surveyDTO.setCode(project.getCode());
        surveyDTO.setTitle(project.getTitle());
        surveyDTO.setCreator(project.getCreator());
//        surveyDTO.setCompanyId(project.getCompanyId());
        surveyDTO.setGroupList(project.getGroupListList());
        surveyDTO.setVersion(project.getVersion());
        surveyDTO.setReleaseVer(project.getReleaseVer());
        surveyDTO.setStatus(project.getStatus().getNumber());
//        surveyDTO.setStructId(project.getStructId());
//        surveyDTO.setLogicId(project.getLogicId());
        surveyDTO.setCustomAttr(project.getCustomAttr());
        surveyDTO.setSource(project.getSource());
//        surveyDTO.setExtId(project.getExtId());
        surveyDTO.setTimerExpire(project.getTimerExpire());
        surveyDTO.setPType(project.getPtype().getNumber());
        surveyDTO.setIsCopyDemo(project.getIsCopyDemo());
        return surveyDTO;
    }
}
